package com.soo.hoo.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soo.hoo.admin.mapper.SysBookReceivingMapper;
import com.soo.hoo.admin.model.entity.SysBookReceiving;
import com.soo.hoo.admin.model.entity.SysBookReciprocation;
import com.soo.hoo.admin.service.BookReceivingService;
import com.soo.hoo.core.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 收礼记录 服务实现类
 * </p>
 *
 * @author 杨燚
 * @since 2024-04-20
 */
@Service
public class BookReceivingServiceImpl extends ServiceImpl<SysBookReceivingMapper, SysBookReceiving> implements BookReceivingService {


    @Override
    @SneakyThrows({IOException.class})
    public void saveDataFromExcel(Long bookId, MultipartFile file) {
        EasyExcel.read(
                file.getInputStream(),
                SysBookReceiving.class,
                new PageReadListener<SysBookReceiving>(dataList -> {
                    dataList.forEach(e -> e.setBookId(bookId));
                    baseMapper.selectBatchIds(dataList);
                })
        ).sheet(0).doRead();
    }


    @Override
    @SneakyThrows({UnsupportedEncodingException.class, IOException.class})
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response) {
        String fileName = URLEncoder.encode("礼簿清单模板", StandardCharsets.UTF_8);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SysBookReceiving.class)
                .sheet("礼簿清单")
                .doWrite(ListUtil.empty());
    }


    @Override
    public Map<Long, Integer> countReceiving(List<Long> bookIds) {
        List<SysBookReceiving> receivingList = baseMapper.selectList(
                new LambdaQueryWrapper<SysBookReceiving>()
                        .in(SysBookReceiving::getBookId, bookIds)
        );
        return receivingList.stream().
                collect(Collectors.groupingBy(
                                SysBookReceiving::getBookId,
                                Collectors.summingInt(e -> 1)
                        )
                );
    }

    @Override
    public Map<Long, List<SysBookReceiving>> listReceiving(List<Long> bookIds) {
        List<SysBookReceiving> receivingList = baseMapper.selectList(
                new LambdaQueryWrapper<SysBookReceiving>()
                        .in(SysBookReceiving::getBookId, bookIds)
        );
        return receivingList.stream()
                .collect(Collectors.groupingBy(SysBookReceiving::getBookId));
    }

    @Override
    public void updateReciprocated(Long receivingId, List<SysBookReciprocation> reciprocationList) {
        boolean reciprocated = CollUtil.isNotEmpty(reciprocationList);
        this.updateReciprocated(receivingId, reciprocated);
    }

    @Override
    public void updateReciprocated(Long receivingId, Boolean reciprocated) {
        SysBookReceiving receiving = baseMapper.selectById(receivingId);
        receiving.setReciprocated(reciprocated);
        baseMapper.updateById(receiving);
    }
}
